function lengthOfLIS(nums) {
    if (nums.length === 0) return 0;

    const tails = [];

    for (let x of nums) {
        let left = 0, right = tails.length;
        while (left < right) {
            const mid = Math.floor((left + right) / 2);
            if (tails[mid] < x) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        if (left === tails.length) {
            tails.push(x);
        } else {
            tails[left] = x;
        }
    }
    console.log("tails：", tails);
    return tails.length;
}

// 示例
const nums = [10, 9, 2, 5, 3, 7, 101, 18];
console.log(lengthOfLIS(nums)); // 输出: 4
